﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElfTyping.States.GameState.PlayerSpells;
using Microsoft.Xna.Framework;

namespace ElfTyping.States.GameState.Entities
{
    public class SpellIndicatorEntity : WordEntity
    {
        float remainingDeactivationTime = 0f;

        private PlayerSpell Spell { get; set; }

        public SpellIndicatorEntity(GameScript script, Vector2 centerPosition, PlayerSpell spell)
            : base(script, centerPosition)
        {
            Spell = spell;            
            CurrentAnimation = spell.IndicatorAnimation;
            MaximumHealth = spell.ShotCoolDown;
            CurrentHealth = 0;
            Word = "";
            HealthBar.HealthColor = Color.Green;
            HealthBar.HurtColor = Color.White;
        }

        public override AcceptLetterResult AcceptLetter()
        {
            if (remainingDeactivationTime > 0)
            {
                return AcceptLetterResult.Failed;
            }

            if (CurrentHealth < MaximumHealth)
            {
                return AcceptLetterResult.Failed;
            }

            return base.AcceptLetter();
        }

        public override void GainHealth(int health)
        {
            if (remainingDeactivationTime > 0)
            {
                return;
            }

            base.GainHealth(health);

            if (CurrentHealth == MaximumHealth && Word == "")
            {
                Word = Spell.GetNewWord();
            }
        }

        protected override void OnWordFinish()
        {
            Script.CastSpell(Spell);
            CurrentHealth = 0;
            Word = "";
            remainingDeactivationTime = Spell.DeactivationTime;

            base.OnWordFinish();
        }

        public override void Tick(float dt)
        {
            if (remainingDeactivationTime > 0)
            {
                remainingDeactivationTime -= dt;
            }
            base.Tick(dt);
        }

        public override void Draw(General.Camera camera)
        {
            if (remainingDeactivationTime <= 0)
            {
                base.Draw(camera);
            }
        }
    }
}
